//
//  BooksVC.m
//  sweetbox
//
//  Created by apollo on 15/9/5.
//  Copyright (c) 2015年 proj-m. All rights reserved.
//

#import "BooksVC.h"
#import "log.h"
#import "DBHelper.h"
#import "ScriptsVC.h"

@interface BooksVC ()
@property (nonatomic, strong) NSMutableArray *books;
- (void) refreshWords;
- (void)onSaveBook;
@end

@implementation BooksVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self
           selector:@selector(onSaveBook)
               name:@"save_book"
             object:nil];
    
    self.books = [[NSMutableArray alloc] init];
    UITableView *view = (UITableView *) self.view;
    view.dataSource = self;
    view.delegate = self;
    [self refreshWords];

}

- (void)onSaveBook {
    [self refreshWords];
    UITableView *view = (UITableView *) self.view;
    [view reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)onNew:(id)sender {
    UIViewController *vc  =(UIViewController *) [[UIStoryboard storyboardWithName:@"prescription" bundle:nil] instantiateViewControllerWithIdentifier:@"new_book"];
    if (nil != vc) {
        vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;  
        [self presentViewController:vc animated:YES completion:^(){
            LOG_I("After Come into NewBook Page");
        }];
    }
}

- (void) refreshWords {
    [self.books removeAllObjects];
    int bookNum = [[DBHelper sharedDBHelper] countBooks];
    for (int i=0;i<bookNum;i++) {
        NSArray *book = [[DBHelper sharedDBHelper] bookAt:i];
        [self.books addObject:book];
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.books.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    NSArray *book = self.books[indexPath.row];
    cell.textLabel.text = book[0];
    cell.detailTextLabel.text = book[1];
    cell.selectionStyle = UITableViewCellSelectionStyleNone; // set cell not height by can by touch on tableview
    cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator; // jump tringle icon
    return cell;
}


#pragma mark - UITableDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"select at %d row",(int)indexPath.row);

    ScriptsVC *vc  =(ScriptsVC *) [[UIStoryboard storyboardWithName:@"prescription" bundle:nil] instantiateViewControllerWithIdentifier:@"scripts"];
    UINavigationController *navVC  =  (UINavigationController *)self.parentViewController;
    vc.book = [self.books objectAtIndex:indexPath.row][0];
    [navVC pushViewController:vc animated:YES];

}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSArray *book = self.books[indexPath.row];
        if (nil == book) {
            LOG_E("Cur row not in self.books");
            return ;
        }
        [[DBHelper sharedDBHelper] deleteBook:book[0]];
        [self.books removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
    }
}
@end
